# coding:utf-8
# -*- coding: utf-8 -*-
# @Time : 2020/9/10 13:59
# @Author: 王鹏-wangpeng99
# @File : ChanMaker.py

import random
from service.ServicelstFactory import ServicelstFactory

from utils import logutils

logger = logutils.getlogger(__file__)

"""
调用链生成工具类
"""


class ChanMaker:
    svcfactory = None

    def __init__(self, horizon=10, vertical=11, chancnt=5):
        self.svcfactory = ServicelstFactory(horizon, vertical)
        self._chancnt = chancnt

    def mkchan(self):
        svclst = self.svcfactory.mksvclst()
        chandict = dict()
        # 重试次数
        trycnt = 3
        while len(chandict) < self._chancnt and trycnt > 0:
            tmplst = []
            for hidx in range(len(svclst)):
                if hidx!=0 and hidx!=len(svclst[0]):
                    randval = random.randint(0,10)
                    if randval%3==0:
                        continue
                tmplst.append(random.choice(svclst[hidx]))
            chankey = '-'.join([item['servicecode'] for item in tmplst])
            if chankey not in chandict:
                chandict[chankey] = tmplst
                trycnt = 3
            else:
                trycnt = trycnt - 1
        logger.debug("========================================")
        for chankey, chanval in chandict.items():
            logger.info("chankey=" + chankey)
            logger.debug("chanval=" + '->'.join([str(item) for item in chanval]))

        return chandict
